﻿namespace CardManagement
{
    using CardManagement.Language;
    using Newtonsoft.Json;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Globalization;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Runtime.CompilerServices;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    using System.Windows.Forms;

    public class MainForm : Form
    {
        public static int m_UserID = -1;
        public int m_lGetCardCfgHandle = -1;
        public int m_lSetCardCfgHandle = -1;
        public int m_lDelCardCfgHandle = -1;
        public string cfgpath = Application.StartupPath + @"\Config.json";
        public string cardpath = Application.StartupPath + @"\CardPath.json";
        private bool sendCard = false;
        private bool XiaFaing = false;
        private IContainer components = null;
        private Label label1;
        private Panel panel1;
        private Button btnLogin;
        private Label label2;
        private TextBox textBoxCardNo;
        private Label label3;
        private TextBox textBoxCardRightPlan;
        private Label label4;
        private TextBox textBoxEmployeeNo;
        private Label label5;
        private TextBox textBoxName;
        private Button btnSet;
        private Button btnGet;
        private Button btnDelete;
        private Label label6;
        private Label label7;
        private Label label8;
        private Label label9;
        private Label label10;
        private Label label11;
        private Label label12;
        private ComboBox comboBoxLanguage;
        private System.Windows.Forms.Timer timerMain;
        private Button buttonSetAuto;
        private TextBox textBoxResult;
        private TextBox textBoxHm;
        private Label label13;
        private Label lblTime;
        private System.Windows.Forms.Timer timerS;
        private Button buttonHas;

        public MainForm()
        {
            InitializeComponent();
            if (!CHCNetSDK.NET_DVR_Init())
            {
                MessageBox.Show("NET_DVR_Init error!");
            }
            else
            {
                comboBoxLanguage.SelectedIndex = 0;
                CHCNetSDK.NET_DVR_SetLogToFile(3, "./", false);
                cfg = GetSysCfg();
                cardHas = GetCardHas();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (m_lDelCardCfgHandle != -1 && CHCNetSDK.NET_DVR_StopRemoteConfig(m_lDelCardCfgHandle))
            {
                m_lDelCardCfgHandle = -1;
            }
            CHCNetSDK.NET_DVR_CARD_COND structure = new CHCNetSDK.NET_DVR_CARD_COND();
            structure.Init();
            structure.dwSize = (uint)Marshal.SizeOf(structure);
            structure.dwCardNum = 1;
            IntPtr ptr = Marshal.AllocHGlobal((int)structure.dwSize);
            Marshal.StructureToPtr(structure, ptr, false);
            CHCNetSDK.NET_DVR_CARD_SEND_DATA net_dvr_card_send_data = new CHCNetSDK.NET_DVR_CARD_SEND_DATA();
            net_dvr_card_send_data.Init();
            net_dvr_card_send_data.dwSize = (uint)Marshal.SizeOf(net_dvr_card_send_data);
            byte[] bytes = new byte[0x20];
            bytes = Encoding.UTF8.GetBytes(textBoxCardNo.Text);
            int index = 0;
            while (true)
            {
                if (index >= bytes.Length)
                {
                    IntPtr ptr2 = Marshal.AllocHGlobal((int)net_dvr_card_send_data.dwSize);
                    Marshal.StructureToPtr(net_dvr_card_send_data, ptr2, false);
                    CHCNetSDK.NET_DVR_CARD_STATUS net_dvr_card_status = new CHCNetSDK.NET_DVR_CARD_STATUS();
                    net_dvr_card_status.Init();
                    net_dvr_card_status.dwSize = (uint)Marshal.SizeOf(net_dvr_card_status);
                    IntPtr ptr3 = Marshal.AllocHGlobal((int)net_dvr_card_status.dwSize);
                    Marshal.StructureToPtr(net_dvr_card_status, ptr3, false);
                    m_lGetCardCfgHandle = CHCNetSDK.NET_DVR_StartRemoteConfig(m_UserID, 0xa02, ptr, (int)structure.dwSize, null, Handle);
                    if (m_lGetCardCfgHandle < 0)
                    {
                        MessageBox.Show("NET_DVR_DEL_CARD error:" + CHCNetSDK.NET_DVR_GetLastError().ToString());
                        Marshal.FreeHGlobal(ptr);
                    }
                    else
                    {
                        int num3 = 0x3e8;
                        uint dwOutDataLen = 0;
                        while (true)
                        {
                            num3 = CHCNetSDK.NET_DVR_SendWithRecvRemoteConfig(m_lGetCardCfgHandle, ptr2, net_dvr_card_send_data.dwSize, ptr3, net_dvr_card_status.dwSize, ref dwOutDataLen);
                            if (num3 == 0x3e9)
                            {
                                Thread.Sleep(10);
                                continue;
                            }
                            if (num3 == 0x3eb)
                            {
                                MessageBox.Show("NET_DVR_DEL_CARD fail error: " + CHCNetSDK.NET_DVR_GetLastError().ToString());
                                continue;
                            }
                            if (num3 != 0x3e8)
                            {
                                if (num3 == 0x3ea)
                                {
                                    MessageBox.Show("NET_DVR_DEL_CARD finish");
                                }
                                else if (num3 == 0x3ec)
                                {
                                    MessageBox.Show("NET_DVR_DEL_CARD exception error: " + CHCNetSDK.NET_DVR_GetLastError().ToString());
                                }
                                else
                                {
                                    MessageBox.Show("unknown status error: " + CHCNetSDK.NET_DVR_GetLastError().ToString());
                                }
                                CHCNetSDK.NET_DVR_StopRemoteConfig(m_lDelCardCfgHandle);
                                m_lDelCardCfgHandle = -1;
                                Marshal.FreeHGlobal(ptr2);
                                Marshal.FreeHGlobal(ptr3);
                                break;
                            }
                            MessageBox.Show("NET_DVR_DEL_CARD success");
                        }
                    }
                    return;
                }
                net_dvr_card_send_data.byCardNo[index] = bytes[index];
                index++;
            }
        }

        private void btnGet_Click(object sender, EventArgs e)
        {
            if (m_lGetCardCfgHandle != -1 && CHCNetSDK.NET_DVR_StopRemoteConfig(m_lGetCardCfgHandle))
            {
                m_lGetCardCfgHandle = -1;
            }
            CHCNetSDK.NET_DVR_CARD_COND structure = new CHCNetSDK.NET_DVR_CARD_COND();
            structure.Init();
            structure.dwSize = (uint)Marshal.SizeOf(structure);
            structure.dwCardNum = 1;
            IntPtr ptr = Marshal.AllocHGlobal((int)structure.dwSize);
            Marshal.StructureToPtr(structure, ptr, false);
            CHCNetSDK.NET_DVR_CARD_RECORD net_dvr_card_record = new CHCNetSDK.NET_DVR_CARD_RECORD();
            net_dvr_card_record.Init();
            net_dvr_card_record.dwSize = (uint)Marshal.SizeOf(net_dvr_card_record);
            byte[] bytes = new byte[0x20];
            bytes = Encoding.UTF8.GetBytes(textBoxCardNo.Text);
            int index = 0;
            while (true)
            {
                if (index >= bytes.Length)
                {
                    IntPtr ptr2 = Marshal.AllocHGlobal((int)net_dvr_card_record.dwSize);
                    Marshal.StructureToPtr(net_dvr_card_record, ptr2, false);
                    CHCNetSDK.NET_DVR_CARD_SEND_DATA net_dvr_card_send_data = new CHCNetSDK.NET_DVR_CARD_SEND_DATA();
                    net_dvr_card_send_data.Init();
                    net_dvr_card_send_data.dwSize = (uint)Marshal.SizeOf(net_dvr_card_send_data);
                    int num2 = 0;
                    while (true)
                    {
                        if (num2 >= bytes.Length)
                        {
                            IntPtr ptr3 = Marshal.AllocHGlobal((int)net_dvr_card_send_data.dwSize);
                            Marshal.StructureToPtr(net_dvr_card_send_data, ptr3, false);
                            m_lGetCardCfgHandle = CHCNetSDK.NET_DVR_StartRemoteConfig(m_UserID, 0xa00, ptr, (int)structure.dwSize, null, Handle);
                            if (m_lGetCardCfgHandle < 0)
                            {
                                MessageBox.Show("NET_DVR_GET_CARD error: " + CHCNetSDK.NET_DVR_GetLastError().ToString());
                                Marshal.FreeHGlobal(ptr);
                            }
                            else
                            {
                                int num4 = 0x3e8;
                                uint dwOutDataLen = 0;
                                while (true)
                                {
                                    num4 = CHCNetSDK.NET_DVR_SendWithRecvRemoteConfig(m_lGetCardCfgHandle, ptr3, net_dvr_card_send_data.dwSize, ptr2, net_dvr_card_record.dwSize, ref dwOutDataLen);
                                    net_dvr_card_record = (CHCNetSDK.NET_DVR_CARD_RECORD)Marshal.PtrToStructure(ptr2, typeof(CHCNetSDK.NET_DVR_CARD_RECORD));
                                    if (num4 == 0x3e9)
                                    {
                                        Thread.Sleep(10);
                                        continue;
                                    }
                                    if (num4 == 0x3eb)
                                    {
                                        MessageBox.Show("NET_DVR_GET_CARD fail error: " + CHCNetSDK.NET_DVR_GetLastError().ToString());
                                        continue;
                                    }
                                    if (num4 != 0x3e8)
                                    {
                                        if (num4 == 0x3ea)
                                        {
                                            MessageBox.Show("NET_DVR_GET_CARD finish");
                                        }
                                        else if (num4 == 0x3ec)
                                        {
                                            MessageBox.Show("NET_DVR_GET_CARD exception error: " + CHCNetSDK.NET_DVR_GetLastError().ToString());
                                        }
                                        else
                                        {
                                            MessageBox.Show("unknown status error: " + CHCNetSDK.NET_DVR_GetLastError().ToString());
                                        }
                                        CHCNetSDK.NET_DVR_StopRemoteConfig(m_lGetCardCfgHandle);
                                        m_lGetCardCfgHandle = -1;
                                        Marshal.FreeHGlobal(ptr3);
                                        Marshal.FreeHGlobal(ptr2);
                                        break;
                                    }
                                    textBoxCardNo.Text = Encoding.Default.GetString(net_dvr_card_record.byCardNo);
                                    textBoxCardRightPlan.Text = net_dvr_card_record.wCardRightPlan[0].ToString();
                                    textBoxEmployeeNo.Text = net_dvr_card_record.dwEmployeeNo.ToString();
                                    textBoxName.Text = Encoding.Default.GetString(net_dvr_card_record.byName);
                                    MessageBox.Show("NET_DVR_GET_CARD success");
                                }
                            }
                            return;
                        }
                        net_dvr_card_send_data.byCardNo[num2] = bytes[num2];
                        num2++;
                    }
                }
                net_dvr_card_record.byCardNo[index] = bytes[index];
                index++;
            }
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            AddDevice device = new AddDevice(cfg);
            device.ShowDialog();
            device.Dispose();
        }

        private void btnSet_Click(object sender, EventArgs e)
        {
            XiaFaCard(textBoxCardNo.Text, textBoxName.Text, textBoxCardRightPlan.Text, textBoxEmployeeNo.Text, DateTime.Now.AddDays(1.0));
        }

        private void buttonHas_Click(object sender, EventArgs e)
        {
            new FormCardHas().Show();
        }

        private void buttonSetAuto_Click(object sender, EventArgs e)
        {
            if (m_UserID < 0)
            {
                MessageBox.Show("请先登录设备！");
            }
            else
            {
                timerMain.Enabled = !timerMain.Enabled;
                timerMain.Interval = int.Parse(textBoxHm.Text);
                buttonSetAuto.Text = timerMain.Enabled ? "关闭自动" : "开启自动";
                textBoxHm.Enabled = !timerMain.Enabled;
            }
        }

        private void CardManagement_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (m_UserID >= 0)
            {
                CHCNetSDK.NET_DVR_Logout_V30(m_UserID);
                m_UserID = -1;
            }
            CHCNetSDK.NET_DVR_Cleanup();
        }

        private void CardManagement_Load(object sender, EventArgs e)
        {
        }

        private void comboBoxLanguage_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBoxLanguage.Text != null)
            {
                MultiLanguage.SetDefaultLanguage(comboBoxLanguage.Text);
                foreach (Form form in Application.OpenForms)
                {
                    MultiLanguage.LoadLanguage(form);
                }
                if (comboBoxLanguage.Text == "English")
                {
                    Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
                }
                else if (comboBoxLanguage.Text == "Chinese")
                {
                    Thread.CurrentThread.CurrentUICulture = new CultureInfo("zh-CN");
                }
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && components != null)
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        public List<Cards> GetCardHas()
        {
            List<Cards> list2;
            string str = FileHelper.TxtRead(cardpath);
            if (!string.IsNullOrEmpty(str))
            {
                list2 = JsonConvert.DeserializeObject<List<Cards>>(str);
            }
            else
            {
                List<Cards> list = new List<Cards>();
                FileHelper.TxtWrite(cardpath, JsonConvert.SerializeObject(list));
                list2 = list;
            }
            return list2;
        }

        private AbpResponse<List<Cards>> GetFckUsers()
        {
            cardHas = GetCardHas();
            AbpResponse<List<Cards>> response = new AbpResponse<List<Cards>>();
            FckResponse<List<FckCard>> response2 = JsonConvert.DeserializeObject<FckResponse<List<FckCard>>>(HttpGet(cfg.ServerURL, "SkipCount=0&MaxResultCount=100"));
            if (response2.datas != null)
            {
                response.success = true;
                List<Cards> list = new List<Cards>();
                string[] cardss = (from o in cardHas select o.CardNo).ToArray();
                foreach (FckCard card in (from o in response2.datas
                                          where !cardss.Contains(o.CardId.ToString())
                                          select o).ToList())
                {
                    string str2 = (card.CardId / 0x10000 % 0x100).ToString() + (card.CardId % 0x10000).ToString();
                    int num = 8 - str2.Length;
                    int num3 = 1;
                    while (true)
                    {
                        if (num3 > num)
                        {
                            card.WgCardNo = str2;
                            Cards item = new Cards();
                            item.CardNo = card.CardId.ToString();
                            item.Name = card.MemberName;
                            item.WgCardNo = str2;
                            item.EndTime = DateTime.Now.AddDays(1.0);
                            list.Add(item);
                            break;
                        }
                        str2 = "0" + str2;
                        num3++;
                    }
                }
                response.result = list;
            }
            return response;
        }

        public SysConfig GetSysCfg()
        {
            SysConfig config2;
            string str = FileHelper.TxtRead(cfgpath);
            if (!string.IsNullOrEmpty(str))
            {
                config2 = JsonConvert.DeserializeObject<SysConfig>(str);
            }
            else
            {
                SysConfig config = new SysConfig();
                FileHelper.TxtWrite(cfgpath, JsonConvert.SerializeObject(config));
                config2 = config;
            }
            return config2;
        }

        private AbpResponse<List<Cards>> GetUsers()
        {
            AbpResponse<List<Cards>> response = JsonConvert.DeserializeObject<AbpResponse<List<Cards>>>(HttpGet(cfg.ServerURL, "SkipCount=0&MaxResultCount=100"));
            if (response.result != null)
            {
                foreach (Cards cards in response.result)
                {
                    string str2 = (int.Parse(cards.CardNo) / 0x10000 % 0x100).ToString() + (int.Parse(cards.CardNo) % 0x10000).ToString();
                    int num = 8 - str2.Length;
                    int num3 = 1;
                    while (true)
                    {
                        if (num3 > num)
                        {
                            cards.WgCardNo = str2;
                            break;
                        }
                        str2 = "0" + str2;
                        num3++;
                    }
                }
            }
            return response;
        }

        public string HttpGet(string Url, string postDataStr)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?") + postDataStr);
            request.Method = "GET";
            request.ContentType = "text/html;charset=UTF-8";
            Stream responseStream = ((HttpWebResponse)request.GetResponse()).GetResponseStream();
            StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
            string str = reader.ReadToEnd();
            reader.Close();
            responseStream.Close();
            return str;
        }

        private string HttpPost(string Url, string postDataStr)
        {
            CookieContainer container = new CookieContainer();
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = Encoding.UTF8.GetByteCount(postDataStr);
            request.CookieContainer = container;
            StreamWriter writer = new StreamWriter(request.GetRequestStream(), Encoding.GetEncoding("gb2312"));
            writer.Write(postDataStr);
            writer.Close();
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            response.Cookies = container.GetCookies(response.ResponseUri);
            Stream responseStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
            string str = reader.ReadToEnd();
            reader.Close();
            responseStream.Close();
            return str;
        }

        private void InitializeComponent()
        {
            components = new Container();
            //ComponentResourceManager manager = new ComponentResourceManager(typeof(CardManagement.CardManagement));
            label1 = new Label();
            panel1 = new Panel();
            btnLogin = new Button();
            label2 = new Label();
            textBoxCardNo = new TextBox();
            label3 = new Label();
            textBoxCardRightPlan = new TextBox();
            label4 = new Label();
            textBoxEmployeeNo = new TextBox();
            label5 = new Label();
            textBoxName = new TextBox();
            btnSet = new Button();
            btnGet = new Button();
            btnDelete = new Button();
            label6 = new Label();
            label7 = new Label();
            label8 = new Label();
            label9 = new Label();
            label10 = new Label();
            label11 = new Label();
            label12 = new Label();
            comboBoxLanguage = new ComboBox();
            timerMain = new System.Windows.Forms.Timer(components);
            buttonSetAuto = new Button();
            textBoxResult = new TextBox();
            textBoxHm = new TextBox();
            label13 = new Label();
            lblTime = new Label();
            timerS = new System.Windows.Forms.Timer(components);
            buttonHas = new Button();
            panel1.SuspendLayout();
            SuspendLayout();
            label1.Anchor = AnchorStyles.Left;
            label1.AutoSize = true;
            label1.BackColor = Color.Transparent;
            label1.Font = new Font("Consolas", 26.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
            label1.ForeColor = Color.White;
            label1.Location = new Point(0xdb, 30);
            label1.Name = "label1";
            label1.Size = new Size(0x12f, 0x29);
            label1.TabIndex = 1;
            label1.Text = "Card Management";
            //this.panel1.BackgroundImage = (Image) manager.GetObject("panel1.BackgroundImage");
            panel1.Controls.Add(label1);
            panel1.Location = new Point(0, -1);
            panel1.Name = "panel1";
            panel1.Size = new Size(0x2d3, 0x6a);
            panel1.TabIndex = 2;
            btnLogin.FlatStyle = FlatStyle.Flat;
            btnLogin.Location = new Point(14, 0x7c);
            btnLogin.Name = "btnLogin";
            btnLogin.Size = new Size(0x67, 0x1d);
            btnLogin.TabIndex = 3;
            btnLogin.Text = "Login";
            btnLogin.UseVisualStyleBackColor = true;
            btnLogin.Click += new EventHandler(btnLogin_Click);
            label2.AutoSize = true;
            label2.BackColor = Color.Transparent;
            label2.Font = new Font("Consolas", 10.5f, FontStyle.Bold, GraphicsUnit.Point, 0);
            label2.ForeColor = Color.DimGray;
            label2.Location = new Point(0xa9, 0x7c);
            label2.Name = "label2";
            label2.Size = new Size(0x48, 0x11);
            label2.TabIndex = 0x2d;
            label2.Text = "Card No:";
            textBoxCardNo.Location = new Point(0x13b, 0x7a);
            textBoxCardNo.Name = "textBoxCardNo";
            textBoxCardNo.Size = new Size(0x9f, 0x16);
            textBoxCardNo.TabIndex = 0x2c;
            textBoxCardNo.Text = "123";
            label3.AutoSize = true;
            label3.BackColor = Color.Transparent;
            label3.Font = new Font("Consolas", 10.5f, FontStyle.Bold, GraphicsUnit.Point, 0);
            label3.ForeColor = Color.DimGray;
            label3.Location = new Point(0xa9, 0x99);
            label3.Name = "label3";
            label3.Size = new Size(0x88, 0x11);
            label3.TabIndex = 0x2f;
            label3.Text = "Card Right Plan:";
            textBoxCardRightPlan.Location = new Point(0x13b, 0x98);
            textBoxCardRightPlan.Name = "textBoxCardRightPlan";
            textBoxCardRightPlan.Size = new Size(0x9f, 0x16);
            textBoxCardRightPlan.TabIndex = 0x2e;
            textBoxCardRightPlan.Text = "test";
            label4.AutoSize = true;
            label4.BackColor = Color.Transparent;
            label4.Font = new Font("Consolas", 10.5f, FontStyle.Bold, GraphicsUnit.Point, 0);
            label4.ForeColor = Color.DimGray;
            label4.Location = new Point(0xa9, 0xb7);
            label4.Name = "label4";
            label4.Size = new Size(0x68, 0x11);
            label4.TabIndex = 0x31;
            label4.Text = "Employee No:";
            textBoxEmployeeNo.Location = new Point(0x13b, 0xb6);
            textBoxEmployeeNo.Name = "textBoxEmployeeNo";
            textBoxEmployeeNo.Size = new Size(0x9f, 0x16);
            textBoxEmployeeNo.TabIndex = 0x30;
            textBoxEmployeeNo.Text = "456";
            label5.AutoSize = true;
            label5.BackColor = Color.Transparent;
            label5.Font = new Font("Consolas", 10.5f, FontStyle.Bold, GraphicsUnit.Point, 0);
            label5.ForeColor = Color.DimGray;
            label5.Location = new Point(0xa9, 0xd6);
            label5.Name = "label5";
            label5.Size = new Size(0x30, 0x11);
            label5.TabIndex = 0x33;
            label5.Text = "Name:";
            textBoxName.Location = new Point(0x13b, 0xd5);
            textBoxName.Name = "textBoxName";
            textBoxName.Size = new Size(0x9f, 0x16);
            textBoxName.TabIndex = 50;
            textBoxName.Text = "Jack";
            btnSet.FlatStyle = FlatStyle.Flat;
            btnSet.Location = new Point(12, 0xd5);
            btnSet.Name = "btnSet";
            btnSet.Size = new Size(0x67, 0x1d);
            btnSet.TabIndex = 0x34;
            btnSet.Text = "Set";
            btnSet.UseVisualStyleBackColor = true;
            btnSet.Click += new EventHandler(btnSet_Click);
            btnGet.FlatStyle = FlatStyle.Flat;
            btnGet.Location = new Point(12, 0x115);
            btnGet.Name = "btnGet";
            btnGet.Size = new Size(0x67, 0x1d);
            btnGet.TabIndex = 0x35;
            btnGet.Text = "Get";
            btnGet.UseVisualStyleBackColor = true;
            btnGet.Click += new EventHandler(btnGet_Click);
            btnDelete.FlatStyle = FlatStyle.Flat;
            btnDelete.Location = new Point(12, 0x157);
            btnDelete.Name = "btnDelete";
            btnDelete.Size = new Size(0x67, 0x1d);
            btnDelete.TabIndex = 0x36;
            btnDelete.Text = "Delete";
            btnDelete.UseVisualStyleBackColor = true;
            btnDelete.Click += new EventHandler(btnDelete_Click);
            label6.AutoSize = true;
            label6.BackColor = SystemColors.Control;
            label6.Font = new Font("Consolas", 9f, FontStyle.Bold, GraphicsUnit.Point, 0);
            label6.ForeColor = Color.Red;
            label6.Location = new Point(500, 0x7e);
            label6.Name = "label6";
            label6.Size = new Size(0xaf, 14);
            label6.TabIndex = 0x37;
            label6.Text = "Set/Get/Delete Card Need";
            label7.AutoSize = true;
            label7.BackColor = SystemColors.Control;
            label7.Font = new Font("Consolas", 9f, FontStyle.Bold, GraphicsUnit.Point, 0);
            label7.ForeColor = Color.Red;
            label7.Location = new Point(500, 0x9b);
            label7.Name = "label7";
            label7.Size = new Size(0x62, 14);
            label7.TabIndex = 0x38;
            label7.Text = "Set Card Need";
            label8.AutoSize = true;
            label8.BackColor = SystemColors.Control;
            label8.Font = new Font("Consolas", 9f, FontStyle.Bold, GraphicsUnit.Point, 0);
            label8.ForeColor = Color.Red;
            label8.Location = new Point(500, 0xb9);
            label8.Name = "label8";
            label8.Size = new Size(0x62, 14);
            label8.TabIndex = 0x39;
            label8.Text = "Set Card Need";
            label9.AutoSize = true;
            label9.BackColor = SystemColors.Control;
            label9.Font = new Font("Consolas", 9f, FontStyle.Bold, GraphicsUnit.Point, 0);
            label9.ForeColor = Color.Red;
            label9.Location = new Point(500, 0xd9);
            label9.Name = "label9";
            label9.Size = new Size(0x62, 14);
            label9.TabIndex = 0x3a;
            label9.Text = "Set Card Need";
            label10.AutoSize = true;
            label10.BackColor = SystemColors.Control;
            label10.Font = new Font("Consolas", 9f, FontStyle.Bold, GraphicsUnit.Point, 0);
            label10.ForeColor = Color.Red;
            label10.Location = new Point(12, 250);
            label10.Name = "label10";
            label10.Size = new Size(0x85, 14);
            label10.TabIndex = 0x3b;
            label10.Text = "Set Card To Device";
            label11.AutoSize = true;
            label11.BackColor = SystemColors.Control;
            label11.Font = new Font("Consolas", 9f, FontStyle.Bold, GraphicsUnit.Point, 0);
            label11.ForeColor = Color.Red;
            label11.Location = new Point(11, 0x13d);
            label11.Name = "label11";
            label11.Size = new Size(0x70, 14);
            label11.TabIndex = 60;
            label11.Text = "Get Device Card";
            label12.AutoSize = true;
            label12.BackColor = SystemColors.Control;
            label12.Font = new Font("Consolas", 9f, FontStyle.Bold, GraphicsUnit.Point, 0);
            label12.ForeColor = Color.Red;
            label12.Location = new Point(9, 0x180);
            label12.Name = "label12";
            label12.Size = new Size(0x85, 14);
            label12.TabIndex = 0x3d;
            label12.Text = "Delete Device Card";
            comboBoxLanguage.FormattingEnabled = true;
            object[] items = new object[] { "English", "Chinese" };
            comboBoxLanguage.Items.AddRange(items);
            comboBoxLanguage.Location = new Point(0x268, 0x1aa);
            comboBoxLanguage.Name = "comboBoxLanguage";
            comboBoxLanguage.Size = new Size(0x5e, 0x16);
            comboBoxLanguage.TabIndex = 0x3e;
            comboBoxLanguage.SelectedIndexChanged += new EventHandler(comboBoxLanguage_SelectedIndexChanged);
            timerMain.Interval = 0xea60;
            timerMain.Tick += new EventHandler(timerMain_Tick);
            buttonSetAuto.Location = new Point(12, 0xa6);
            buttonSetAuto.Name = "buttonSetAuto";
            buttonSetAuto.Size = new Size(0x69, 0x22);
            buttonSetAuto.TabIndex = 0x3f;
            buttonSetAuto.Text = "自动抓取";
            buttonSetAuto.UseVisualStyleBackColor = true;
            buttonSetAuto.Click += new EventHandler(buttonSetAuto_Click);
            textBoxResult.Enabled = false;
            textBoxResult.Location = new Point(0x9c, 250);
            textBoxResult.Multiline = true;
            textBoxResult.Name = "textBoxResult";
            textBoxResult.ScrollBars = ScrollBars.Vertical;
            textBoxResult.Size = new Size(0x22a, 0x9c);
            textBoxResult.TabIndex = 0x40;
            textBoxHm.Location = new Point(0x110, 0x1aa);
            textBoxHm.Name = "textBoxHm";
            textBoxHm.Size = new Size(100, 0x16);
            textBoxHm.TabIndex = 0x41;
            textBoxHm.Text = "60000";
            label13.AutoSize = true;
            label13.Location = new Point(0xa1, 0x1ad);
            label13.Name = "label13";
            label13.Size = new Size(0x69, 14);
            label13.TabIndex = 0x42;
            label13.Text = "自动抓取频率：";
            lblTime.AutoSize = true;
            lblTime.Location = new Point(0x17a, 0x1ad);
            lblTime.Name = "lblTime";
            lblTime.Size = new Size(70, 14);
            lblTime.TabIndex = 0x43;
            lblTime.Text = "lableTime";
            timerS.Enabled = true;
            timerS.Interval = 0x3e8;
            timerS.Tick += new EventHandler(timerS_Tick);
            buttonHas.Location = new Point(12, 0x199);
            buttonHas.Name = "buttonHas";
            buttonHas.Size = new Size(0x67, 0x22);
            buttonHas.TabIndex = 0x44;
            buttonHas.Text = "已有卡";
            buttonHas.UseVisualStyleBackColor = true;
            buttonHas.Click += new EventHandler(buttonHas_Click);
            AutoScaleDimensions = new SizeF(7f, 14f);
            AutoScaleMode = AutoScaleMode.Font;
            ClientSize = new Size(0x2d2, 460);
            Controls.Add(buttonHas);
            Controls.Add(lblTime);
            Controls.Add(label13);
            Controls.Add(textBoxHm);
            Controls.Add(textBoxResult);
            Controls.Add(buttonSetAuto);
            Controls.Add(comboBoxLanguage);
            Controls.Add(label12);
            Controls.Add(label11);
            Controls.Add(label10);
            Controls.Add(label9);
            Controls.Add(label8);
            Controls.Add(label7);
            Controls.Add(label6);
            Controls.Add(btnDelete);
            Controls.Add(btnGet);
            Controls.Add(btnSet);
            Controls.Add(label5);
            Controls.Add(textBoxName);
            Controls.Add(label4);
            Controls.Add(textBoxEmployeeNo);
            Controls.Add(label3);
            Controls.Add(textBoxCardRightPlan);
            Controls.Add(label2);
            Controls.Add(textBoxCardNo);
            Controls.Add(btnLogin);
            Controls.Add(panel1);
            Font = new Font("Consolas", 9f, FontStyle.Regular, GraphicsUnit.Point, 0);
            FormBorderStyle = FormBorderStyle.FixedSingle;
            //base.Icon = (Icon) manager.GetObject("$this.Icon");
            MaximizeBox = false;
            Name = "CardManagement";
            StartPosition = FormStartPosition.CenterScreen;
            Text = "海康门禁卡管理服务";
            FormClosing += new FormClosingEventHandler(CardManagement_FormClosing);
            Load += new EventHandler(CardManagement_Load);
            panel1.ResumeLayout(false);
            panel1.PerformLayout();
            ResumeLayout(false);
            PerformLayout();
        }

        private string SendCardData(string textBoxCardNo, string textBoxName, string textBoxCardRightPlan, string textBoxEmployeeNo, DateTime startTime, DateTime endTime)
        {
            string str = string.Empty;
            sendCard = false;
            CHCNetSDK.NET_DVR_CARD_RECORD structure = new CHCNetSDK.NET_DVR_CARD_RECORD();
            structure.Init();
            structure.dwSize = (uint)Marshal.SizeOf(structure);
            structure.byCardType = 1;
            byte[] bytes = new byte[0x20];
            bytes = Encoding.UTF8.GetBytes(textBoxCardNo);
            int index = 0;
            while (true)
            {
                if (index >= bytes.Length)
                {
                    ushort.TryParse(textBoxCardRightPlan, out structure.wCardRightPlan[0]);
                    uint.TryParse(textBoxEmployeeNo, out structure.dwEmployeeNo);
                    byte[] buffer2 = new byte[0x20];
                    buffer2 = Encoding.Default.GetBytes(textBoxName);
                    int num4 = 0;
                    while (true)
                    {
                        if (num4 >= 0x20)
                        {
                            int num5 = 0;
                            while (true)
                            {
                                if (num5 >= buffer2.Length)
                                {
                                    structure.struValid.byEnable = 1;
                                    structure.struValid.struBeginTime.wYear = ushort.Parse(startTime.Year.ToString());
                                    structure.struValid.struBeginTime.byMonth = byte.Parse(startTime.Month.ToString());
                                    structure.struValid.struBeginTime.byDay = byte.Parse(startTime.Day.ToString());
                                    structure.struValid.struBeginTime.byHour = byte.Parse(startTime.Hour.ToString());
                                    structure.struValid.struBeginTime.byMinute = byte.Parse(startTime.Minute.ToString());
                                    structure.struValid.struBeginTime.bySecond = byte.Parse(startTime.Second.ToString());
                                    structure.struValid.struEndTime.wYear = ushort.Parse(endTime.Year.ToString());
                                    structure.struValid.struEndTime.byMonth = byte.Parse(endTime.Month.ToString());
                                    structure.struValid.struEndTime.byDay = byte.Parse(endTime.Day.ToString());
                                    structure.struValid.struEndTime.byHour = byte.Parse(endTime.Hour.ToString());
                                    structure.struValid.struEndTime.byMinute = byte.Parse(endTime.Minute.ToString());
                                    structure.struValid.struEndTime.bySecond = byte.Parse(endTime.Second.ToString());
                                    structure.byDoorRight[0] = 1;
                                    structure.byDoorRight[1] = 1;
                                    structure.wCardRightPlan[0] = 1;
                                    structure.wCardRightPlan[1] = 1;
                                    IntPtr ptr = Marshal.AllocHGlobal((int)structure.dwSize);
                                    Marshal.StructureToPtr(structure, ptr, false);
                                    CHCNetSDK.NET_DVR_CARD_STATUS net_dvr_card_status = new CHCNetSDK.NET_DVR_CARD_STATUS();
                                    net_dvr_card_status.Init();
                                    net_dvr_card_status.dwSize = (uint)Marshal.SizeOf(net_dvr_card_status);
                                    IntPtr ptr2 = Marshal.AllocHGlobal((int)net_dvr_card_status.dwSize);
                                    Marshal.StructureToPtr(net_dvr_card_status, ptr2, false);
                                    int num = 0x3e8;
                                    uint dwOutDataLen = 0;
                                    while (true)
                                    {
                                        num = CHCNetSDK.NET_DVR_SendWithRecvRemoteConfig(m_lSetCardCfgHandle, ptr, structure.dwSize, ptr2, net_dvr_card_status.dwSize, ref dwOutDataLen);
                                        net_dvr_card_status = (CHCNetSDK.NET_DVR_CARD_STATUS)Marshal.PtrToStructure(ptr2, typeof(CHCNetSDK.NET_DVR_CARD_STATUS));
                                        if (num == 0x3e9)
                                        {
                                            Thread.Sleep(10);
                                            continue;
                                        }
                                        if (num == 0x3eb)
                                        {
                                            str = str + "NET_DVR_SET_CARD fail error: " + CHCNetSDK.NET_DVR_GetLastError().ToString() + "\r\n";
                                            continue;
                                        }
                                        if (num != 0x3e8)
                                        {
                                            str = num != 0x3ea ? num != 0x3ec ? str + "unknown status error: " + CHCNetSDK.NET_DVR_GetLastError().ToString() + "\r\n" : str + "NET_DVR_SET_CARD exception error: " + CHCNetSDK.NET_DVR_GetLastError().ToString() + "\r\n" : str + "NET_DVR_SET_CARD finish\r\n";
                                            CHCNetSDK.NET_DVR_StopRemoteConfig(m_lSetCardCfgHandle);
                                            m_lSetCardCfgHandle = -1;
                                            Marshal.FreeHGlobal(ptr);
                                            Marshal.FreeHGlobal(ptr2);
                                            return str;
                                        }
                                        if (net_dvr_card_status.dwErrorCode != 0)
                                        {
                                            str = str + "NET_DVR_SET_CARD success but errorCode:" + net_dvr_card_status.dwErrorCode.ToString() + "\r\n";
                                            continue;
                                        }
                                        sendCard = true;
                                        string[] textArray1 = new string[] { str, textBoxCardNo, " NET_DVR_SET_CARD success", net_dvr_card_status.dwErrorCode.ToString(), "\r\n" };
                                        str = string.Concat(textArray1);
                                    }
                                }
                                structure.byName[num5] = buffer2[num5];
                                num5++;
                            }
                        }
                        structure.byName[num4] = 0;
                        num4++;
                    }
                }
                structure.byCardNo[index] = bytes[index];
                index++;
            }
        }

        //[AsyncStateMachine(typeof(<timerMain_Tick>d__36)), DebuggerStepThrough]
        private void timerMain_Tick(object sender, EventArgs e)
        {
            //<timerMain_Tick>d__36 stateMachine = new <timerMain_Tick>d__36 {
            //    <>t__builder = AsyncVoidMethodBuilder.Create(),
            //    <>4__this = this,
            //    sender = sender,
            //    e = e,
            //    <>1__state = -1
            //};
            //stateMachine.<>t__builder.Start<<timerMain_Tick>d__36>(ref stateMachine);
        }

        private void timerS_Tick(object sender, EventArgs e)
        {
            lblTime.Text = DateTime.Now.ToLongTimeString();
        }

        public string XiaFaCard(string textBoxCardNo, string textBoxName, string textBoxCardRightPlan, string textBoxEmployeeNo, DateTime endTime)
        {
            string str = DateTime.Now.ToString("G") + " no send cards \r\n";
            if (m_lSetCardCfgHandle != -1 && CHCNetSDK.NET_DVR_StopRemoteConfig(m_lSetCardCfgHandle))
            {
                m_lSetCardCfgHandle = -1;
            }
            CHCNetSDK.NET_DVR_CARD_COND structure = new CHCNetSDK.NET_DVR_CARD_COND();
            structure.Init();
            structure.dwSize = (uint)Marshal.SizeOf(structure);
            structure.dwCardNum = 1;
            IntPtr ptr = Marshal.AllocHGlobal((int)structure.dwSize);
            Marshal.StructureToPtr(structure, ptr, false);
            m_lSetCardCfgHandle = CHCNetSDK.NET_DVR_StartRemoteConfig(m_UserID, 0xa01, ptr, (int)structure.dwSize, null, IntPtr.Zero);
            if (m_lSetCardCfgHandle < 0)
            {
                str = str + "NET_DVR_SET_CARD error:" + CHCNetSDK.NET_DVR_GetLastError().ToString() + "\r\n";
                Marshal.FreeHGlobal(ptr);
            }
            else
            {
                str = SendCardData(textBoxCardNo, textBoxName, textBoxCardRightPlan, textBoxEmployeeNo, DateTime.Now, endTime);
                Marshal.FreeHGlobal(ptr);
            }
            return str;
        }

        public SysConfig cfg { get; set; }

        public List<Cards> _cards { get; set; }

        public List<Cards> cardHas { get; set; }

        //[Serializable, CompilerGenerated]
        //private sealed class <>c
        //{
        //    public static readonly CardManagement.CardManagement.<>c <>9 = new CardManagement.CardManagement.<>c();
        //    public static Func<Cards, string> <>9__29_0;

        //    internal string <GetFckUsers>b__29_0(Cards o) => 
        //        o.CardNo;
        //}

        //[CompilerGenerated]
        //private sealed class <timerMain_Tick>d__36 : IAsyncStateMachine
        //{
        //    public int <>1__state;
        //    public AsyncVoidMethodBuilder <>t__builder;
        //    public object sender;
        //    public EventArgs e;
        //    public CardManagement.CardManagement <>4__this;
        //    private CardManagement.CardManagement.<>c__DisplayClass36_0 <>8__1;
        //    private AbpResponse<List<Cards>> <resp>5__2;
        //    private CardManagement.CardManagement.<>c__DisplayClass36_1 <>8__3;
        //    private List<Cards>.Enumerator <>s__4;
        //    private CardManagement.CardManagement.<>c__DisplayClass36_2 <>8__5;
        //    private TaskAwaiter <>u__1;

        //    //private void MoveNext()
        //    //{
        //    //    int num = this.<>1__state;
        //    //    try
        //    //    {
        //    //        if (num != 0)
        //    //        {
        //    //            this.<>8__1 = new CardManagement.CardManagement.<>c__DisplayClass36_0();
        //    //            this.<>8__1.<>4__this = this.<>4__this;
        //    //            this.<resp>5__2 = this.<>4__this.GetFckUsers();
        //    //            if (this.<resp>5__2.success)
        //    //            {
        //    //                this.<>4__this._cards = this.<resp>5__2.result;
        //    //            }
        //    //            this.<>8__1.result = DateTime.Now.ToString("G") + " no message \r\n";
        //    //            if ((this.<>4__this._cards == null) || this.<>4__this.XiaFaing)
        //    //            {
        //    //                this.<>4__this.XiaFaing = false;
        //    //                goto TR_0006;
        //    //            }
        //    //            else
        //    //            {
        //    //                this.<>8__3 = new CardManagement.CardManagement.<>c__DisplayClass36_1();
        //    //                this.<>8__3.CS$<>8__locals1 = this.<>8__1;
        //    //                this.<>4__this.XiaFaing = true;
        //    //                this.<>8__3.lists = new List<Cards>();
        //    //                this.<>s__4 = this.<>4__this._cards.GetEnumerator();
        //    //            }
        //    //        }
        //    //        try
        //    //        {
        //    //            TaskAwaiter awaiter;
        //    //            if (num == 0)
        //    //            {
        //    //                awaiter = this.<>u__1;
        //    //                this.<>u__1 = new TaskAwaiter();
        //    //                this.<>1__state = num = -1;
        //    //            }
        //    //            else
        //    //            {
        //    //                goto TR_000D;
        //    //            }
        //    //        TR_0009:
        //    //            awaiter.GetResult();
        //    //            this.<>4__this.textBoxResult.Text = this.<>4__this.textBoxResult.Text + this.<>8__5.CS$<>8__locals2.CS$<>8__locals1.result;
        //    //            this.<>4__this.textBoxResult.SelectionStart = this.<>4__this.textBoxResult.TextLength;
        //    //            this.<>4__this.textBoxResult.SelectionLength = 0;
        //    //            this.<>4__this.textBoxResult.ScrollToCaret();
        //    //            this.<>8__5 = null;
        //    //        TR_000D:
        //    //            while (true)
        //    //            {
        //    //                if (this.<>s__4.MoveNext())
        //    //                {
        //    //                    this.<>8__5 = new CardManagement.CardManagement.<>c__DisplayClass36_2();
        //    //                    this.<>8__5.CS$<>8__locals2 = this.<>8__3;
        //    //                    this.<>8__5.card = this.<>s__4.Current;
        //    //                    awaiter = Task.Run(new Action(this.<>8__5.<timerMain_Tick>b__0)).GetAwaiter();
        //    //                    if (awaiter.IsCompleted)
        //    //                    {
        //    //                        goto TR_0009;
        //    //                    }
        //    //                    else
        //    //                    {
        //    //                        this.<>1__state = num = 0;
        //    //                        this.<>u__1 = awaiter;
        //    //                        CardManagement.CardManagement.<timerMain_Tick>d__36 stateMachine = this;
        //    //                        this.<>t__builder.AwaitUnsafeOnCompleted<TaskAwaiter, CardManagement.CardManagement.<timerMain_Tick>d__36>(ref awaiter, ref stateMachine);
        //    //                    }
        //    //                }
        //    //                else
        //    //                {
        //    //                    goto TR_0007;
        //    //                }
        //    //                break;
        //    //            }
        //    //        }
        //    //        finally
        //    //        {
        //    //            if (num < 0)
        //    //            {
        //    //                this.<>s__4.Dispose();
        //    //            }
        //    //        }
        //    //        return;
        //    //    TR_0006:
        //    //        this.<>4__this.textBoxResult.Text = this.<>4__this.textBoxResult.Text + this.<>8__1.result;
        //    //        this.<>4__this.textBoxResult.SelectionStart = this.<>4__this.textBoxResult.TextLength;
        //    //        this.<>4__this.textBoxResult.SelectionLength = 0;
        //    //        this.<>4__this.textBoxResult.ScrollToCaret();
        //    //        FileHelper.TxtWrite(this.<>4__this.cardpath, JsonConvert.SerializeObject(this.<>4__this.cardHas));
        //    //        this.<>1__state = -2;
        //    //        this.<>8__1 = null;
        //    //        this.<resp>5__2 = null;
        //    //        this.<>t__builder.SetResult();
        //    //        return;
        //    //    TR_0007:
        //    //        this.<>s__4 = new List<Cards>.Enumerator();
        //    //        this.<>4__this.cardHas = this.<>4__this.cardHas.Concat<Cards>(this.<>8__3.lists).ToList<Cards>();
        //    //        this.<>8__3 = null;
        //    //        goto TR_0006;
        //    //    }
        //    //    catch (Exception exception)
        //    //    {
        //    //        this.<>1__state = -2;
        //    //        this.<>8__1 = null;
        //    //        this.<resp>5__2 = null;
        //    //        this.<>t__builder.SetException(exception);
        //    //    }
        //    //}

        //    [DebuggerHidden]
        //    private void SetStateMachine(IAsyncStateMachine stateMachine)
        //    {
        //    }
        //}
    }
}

